﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SummaryDate
{
    public class CountDGN
    {
        private List<string> DGNFileList;
        private List<string> DGNFileListOk = new List<string>();
        private List<string> DGNFileListNo = new List<string>();
        private List<string> DGNFileListOther = new List<string>();

        public void SacnPaths(string DGNPath)
        {

            DGNFileList = SacnPath(DGNPath);
        }
        public void GetResult()
        {
            foreach (string fileName in DGNFileList)
            {
                string resultString = string.Empty;
                Regex r = new Regex(@"[\u4e00-\u9fa5]+");
                Match m = r.Match(fileName);
                if (m.Success)
                {
                    resultString = m.Value;

                    if (resultString == "检查不合格")
                    {
                        Console.WriteLine(string.Format("{0}: {1}", resultString, MachFileName(fileName).Replace(".dgn", "")));
                        DGNFileListNo.Add(MachFileName(fileName).Replace(".dgn", "\r\n"));
                    }
                    else if (resultString == "检查合格")
                    {
                        Console.WriteLine(string.Format("{0}: {1}", resultString, MachFileName(fileName).Replace(".dgn", "")));
                        DGNFileListOk.Add(MachFileName(fileName).Replace(".dgn", "\r\n"));
                    }
                    else
                    {
                        Console.WriteLine(string.Format("{0}: {1}", resultString, MachFileName(fileName).Replace(".dgn", "")));
                        DGNFileListOther.Add(resultString + "   " + MachFileName(fileName).Replace(".dgn", "\r\n"));

                    }
                }
            }
            CopyToClipboard();
        }
        private List<string> SacnPath(string DGNPath)
        {

            List<string> Files = new List<string>();
            DirectoryInfo di = new DirectoryInfo(DGNPath);
            FileInfo[] fis = di.GetFiles();
            for (int i = 0; i < fis.Length; i++)
            {
                if (fis[i].Extension.ToLower() == ".dgn")
                {
                    Console.WriteLine("文件名: " + fis[i].Name);
                    Files.Add(fis[i].Name);
                }

            }
            DirectoryInfo[] dis = di.GetDirectories();
            for (int j = 0; j < dis.Length; j++)
            {

                Files.AddRange(SacnPath(dis[j].FullName));
            }


            return Files;
        }
        private void CopyToClipboard()
        {

            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("合格：{0}  不合格：{1} 其他：{2} \r\n ", DGNFileListOk.Count, DGNFileListNo.Count, DGNFileListOther.Count));
            sb.Append("合格" + "\r\n");
            foreach (string item in DGNFileListOk)
            {
                sb.Append(item);
            }
            sb.Append("不合格" + "\r\n");
            foreach (string item in DGNFileListNo)
            {
                sb.Append(item);
            }
            foreach (string item in DGNFileListOther)
            {
                sb.Append(item);
            }
            Clipboard.SetDataObject(sb.ToString(), true);
            Console.WriteLine("信息已复制到粘贴板");
        }
        private string MachFileName(string FileName)
        {

            Regex r = new Regex(@"07122.+\.dgn$");
            Match m = r.Match(FileName);
            return m.Value;

        }

    }
}
